// Copyright 2025 CCC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//默认缓存期限为7天
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;

/**
 *
 * @param {string} prefixKey
 * @param {Storage} [storage =localStorage]
 * @returns
 */
export const createStorage = ({ prefixKey = '', storage = localStorage } = {}) => {
  const Storage = class {
    private storage = storage;
    private prefixKey?: string = prefixKey;

    private getKey(key: string) {
      return `${this.prefixKey}${key}`.toUpperCase();
    }
    /**
     * @description 设置缓存
     * @param {string } key 缓存键
     * @param {*} value 缓存值
     * @param expire
     */
    set(key: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
      const stringData = JSON.stringify({
        value,
        expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
      });
      this.storage.setItem(this.getKey(key), stringData);
    }
    /**
     *
     * @param {string} key 缓存键
     * @param {*=} def 默认值
     * @returns
     */
    get(key: string, def: any = null) {
      const item = this.storage.getItem(this.getKey(key));

      if (item) {
        try {
          const data = JSON.parse(item);
          const { value, expire } = data;

          // 在有效期内直接返回
          if (expire === null || expire >= Date.now()) {
            return value;
          }

          this.remove(this.getKey(key));
        } catch (error) {
          return error;
        }

        return def;
      }
    }
    /**
     *从缓存删除某项
     * @param {string} key
     */
    remove(key: string) {
      this.storage.removeItem(this.getKey(key));
    }
    /**
     * 清空所有缓存
     */
    clear(): void {
      this.storage.clear();
    }
    /**
     *设置cookie
     * @param {string} name cookie名称
     * @param {*} value cookie值
     * @param {number=}expire 过期时间
     * 如果过期时间为设置，默认关闭浏览器自动删除
     */
    setCookie(name: string, value: any, expire: number | null = DEFAULT_CACHE_TIME) {
      document.cookie = `${this.getKey(name)}=${value}; Max-Age${expire}`;
    }
    /**
     *根据名字获取cookie值
     * @param name
     * @returns
     */
    getCookie(name: string): string {
      const cookieArr = document.cookie.split('; ');

      for (let i = 0, length = cookieArr.length; i < length; i++) {
        const kv = cookieArr[i].split('=');

        if (kv[0] === this.getKey(name)) {
          return kv[1];
        }
      }

      return '';
    }
    /**
     * 根据名字删除指定的cookie
     * @param {string} key
     */
    removeCookie(key: string) {
      this.setCookie(key, 1, -1);
    }
    /**
     * 清空所有cookie 使所有cookie失效
     */
    clearCookie(): void {
      const keys = document.cookie.match(/[^ =;]+(?==)/g);

      if (keys) {
        for (let i = keys.length; i--; ) {
          document.cookie = keys[i] + '=0;expire=' + new Date(0).toUTCString();
        }
      }
    }
  };

  return new Storage();
};

export const storage = createStorage();
export default Storage;
